<?php
require_once('../includes/settings.php');
require_once('../includes/functions.php');
require_once('../includes/repository.php');
require_once('../includes/sql.php');
deny_get_request();
deny_anonymous();

$action = '';
$user_id = user_val('id');
if(isset($_REQUEST['action'])){
	$action=strtolower($_REQUEST['action']);

	if($action=='save'){
		$id = isset($_POST['id'])?intval($_POST['id']):0;
		$title = $_POST['title'];
		$content = $_POST['content'];
		$group_id = $_POST['group_id'];
		if($id==0){
			$id = Topic::create($group_id, $user_id, $title, $content);
		}else{
			Topic::update($id, $title, $content,$user_id);
		}
		Repository::commit();
		Repository::close();
		header("location:/group/topic.php?id=".$id);
		exit();
	}elseif ($action=='reply') {
		$id = isset($_POST['id'])?intval($_POST['id']):0;
		$topic_id = intval($_POST['topic_id']);
		$content = $_POST['content'];
		if($id==0){
			Reply::create($topic_id,$user_id,$content);
		}else{
			Reply::update($id,$content,$user_id);
		}
		Repository::commit();
		Repository::close();
		header('location:'.$_SERVER['HTTP_REFERER']);
		exit();
	}elseif($action=="remove_topic"){
		$id = isset($_POST['id'])?intval($_POST['id']):0;
		Topic::remove($id,$user_id);
		Repository::commit();
		Repository::close();
		header('location:/group/topics.php');
		exit();
	}elseif($action=="remove_reply"){
		$id = isset($_POST['id'])?intval($_POST['id']):0;
		Reply::remove($id,$user_id);
		Repository::commit();
		Repository::close();
		header('location:'.$_SERVER['HTTP_REFERER']);
		exit();
	}
}

?>